********************************************************************************
** 	TITLE: External Validity (AT1)
**
**	PURPOSE: Compare sample with NFCS sample, create AT1
**							
**	INPUTS: NFCS 2018 State Data 190603.csv 
**	
**	OUTPUTS: external_validity.xlsx
**			 
**  CREATED/EDITED BY: Leah Kim, Hasan Ahamed
**
**	DATE LAST EDITED: 3/1/2023
********************************************************************************
clear all
local balancevar age1 female1 married1 adults1 child1 race_nonwhite1 college1 bvstress1 bvbills1 revbvneed1 revfinsit1 incgt301

/*******************************************************************************
1. Prepare data
********************************************************************************/

import delimited "$raw_data/NFCS 2018 State Data 190603.csv", clear

*data cleaning
recode j32 (98 99 = .)
gen averageverygood = inlist(j32,3,4,5) if ~missing(j32)
gen verybadbad = inlist(j32,1,2) if ~missing(j32)

egen totalweight = total(wgt_n2)
gen var_weight1 = averageverygood * wgt_n2
gen var_weight2 = verybadbad * wgt_n2
egen totalvar_weight1 = total(var_weight1)
egen totalvar_weight2 = total(var_weight2)
gen weighted1 = totalvar_weight1 / totalweight
gen weighted2 = totalvar_weight2 / totalweight

rename a3ar_w age1
replace age1 = (18 + 24)/2 if age1 == 1
replace age1 = (25 + 34)/2 if age1 == 2
replace age1 = (35 + 44)/2 if age1 == 3
replace age1 = (45 + 54)/2 if age1 == 4
replace age1 = (55 + 64)/2 if age1 == 5
replace age1 = (65 + 90)/2 if age1 == 6
 
rename a3 female1 
recode female1 (1=0) (2=1)
rename a6 married1 
recode married1 (1=1) (2 3 4 5 = 0) (99 = .)

rename a41 college1
recode college1 (1 2 3 4 = 0) (5 6 = 1) (98 99 = .)

*no num adults
rename a11 child1
recode child1 (5 6=0)

rename a4a_new_w race_nonwhite1
recode race_nonwhite1 (2=1) (1=0) 

rename j33_41 bvstress1
recode bvstress1 (1=7) (2=6) (3=5) (7=1) (6=2) (5=3) (98 99 = .)
sum bvstress1
replace bvstress1  = (bvstress1 - r(min))/(r(max)-r(min))

rename j4 bvbills1
recode bvbills1 (98 99 = .)
sum bvbills1 
replace bvbills1  = (bvbills1 - r(min))/(r(max)-r(min))

rename j20 revbvneed1
recode revbvneed1 (4=1) (2=3) (3=2) (1=4) (98 99 = .)
sum revbvneed1
replace revbvneed1 = (revbvneed1 - r(min))/(r(max)-r(min))

rename j1 revfinsit1 
recode revfinsit1 (98 99 = .)
sum revfinsit1 
replace revfinsit1 = (revfinsit1 - r(min))/(r(max)-r(min))

rename a8 incgt301
recode incgt301 (1 2 3 = 0) (4 5 6 7 8 = 1) 

sum age1 female1 married1 child1 bvstress1 bvbills1 revbvneed1 revfinsit1 incgt301
gen surveyid = _n + 17131

/*******************************************************************************
2. External Validity
********************************************************************************/

append using "$adta/Wide with Outcomes.dta", gen(source) 

tab op18 if flag_randomized == 1
replace race_nonwhite1 = ~race_white1 if ~missing(race_white1) & source == 1
keep `balancevar' averageverygood flag_randomized op18 source

sum `balancevar' if source == 1

replace bvstress1 = (revfinsit1 - 1)/4 if source == 1
replace bvbills1 = (bvbills1 - 1)/4 if source == 1
replace revbvneed1 = (revbvneed1 - 1)/4 if source == 1
replace revfinsit1 = (revfinsit1 -1)/4 if source == 1

sum `balancevar' if source == 0
sum `balancevar' if source == 1
matrix define EV=J(`:word count `balancevar''*2,4,.)

qui forval t=1/`:word count `balancevar'' { 
	
	local var `:word `t' of `balancevar''
	local i = `t'*2-1

	sum `var' if flag_randomized == 1
		matrix EV[`i',1] = r(mean)
		matrix EV[`i'+1,1] = r(sd)
	sum `var' if flag_randomized == 1 & op18 == 1
		matrix EV[`i',2] = r(mean)
		matrix EV[`i'+1,2] = r(sd)
	sum `var' if averageverygood == 0
		matrix EV[`i',3] = r(mean)
		matrix EV[`i'+1,3] = r(sd)
	sum `var' if averageverygood == 1 
		matrix EV[`i',4] = r(mean)
		matrix EV[`i'+1,4] = r(sd)
}

putexcel set "$outputtables/external_validity.xlsx", replace
putexcel A1 = matrix(EV), names nformat("#.000")

